package com.shenmazong.serveraopdemo.config;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @program: server-java-demo
 * @description: LoggingAspect
 * @author: 亮子说编程
 * @create: 2020-12-02 08:45
 **/
@Aspect //1
@Component
public class LoggingAspect {

    @Pointcut("@annotation(com.shenmazong.serveraopdemo.config.Logging)") //2
    public void annotationPointCut(){}


    @Before("annotationPointCut()") //3
    public void beforeAnnotationPointCut(JoinPoint joinPoint){//4
        String name = (String) joinPoint.getArgs()[0]; //5
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        String action = methodSignature.getMethod().getAnnotation(Logging.class).value(); //6
        System.out.println("对" + name + "进行了"+ action);
    }

    @AfterReturning(pointcut = "annotationPointCut()", returning = "retName") //7
    public void afterReturningAnnotationPointCut(JoinPoint joinPoint, String retName){
        String name = (String) joinPoint.getArgs()[0];
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        String action = methodSignature.getMethod().getAnnotation(Logging.class).value();
        System.out.println("对" + name + "进行了"+ action + "，返回的名字为：" + retName);
    }

}
